//
//  MainTabBarViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/19.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        
        self.delegate = self
        
        tabBar.isTranslucent = false
        tabBar.tintColor = APPConfig.baseColor
        
        addController(HomePageViewController(),
                      title: "首页".localized,
                      image: AppIcon.image("", size: 24))
        addController(CheckViewController(),
                      title: "查验".localized,
                      image: AppIcon.image("", size: 24))
        addController(ServiceViewController(),
                      title: "反馈".localized,
                      image: AppIcon.image("", size: 24))
        addController(PersonalViewController(),
                      title: "我的".localized,
                      image: AppIcon.image("", size: 24))
        
        self.selectedIndex = 1
        
        let redPoint = UIView()
        redPoint.layer.cornerRadius = 4
        redPoint.backgroundColor = .red
        if tabBar.subviews.count > 2 {
            let batButton = tabBar.subviews[2]
            batButton.addSubview(redPoint)
            redPoint.snp.makeConstraints { (make) in
                make.centerX.equalTo(batButton.snp.centerX).offset(20)
                make.top.equalTo(10)
                make.height.width.equalTo(8)
            }
        }
        APPMessage.shared.unreadMessageCount
            .map{ $0 == 0 }
            .bind(to: redPoint.rx.isHidden)
            .disposed(by: rx.disposeBag)
    }
    
    func addController(_ rootViewController: UIViewController, title: String, image: UIImage?) {
        let tabBarItem = rootViewController.tabBarItem
        tabBarItem?.title = title
        tabBarItem?.image = image
        let navigationController = MainNavigationViewController(rootViewController: rootViewController)
        self.addChild(navigationController)
    }
    
}
